package com.ruoyi.teachingclass.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.grade.domain.Grade;
import com.ruoyi.teachingclass.domain.StudentVO;
import com.ruoyi.teachingclass.domain.TeachingClassVO;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teachingclass.domain.Teachingclass;
import com.ruoyi.teachingclass.service.ITeachingclassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教学班信息Controller
 *
 * @author LinBei
 * @date 2025-05-16
 */
@RestController
@RequestMapping("/teachingclass/teachingclass")
public class TeachingclassController extends BaseController
{
    @Autowired
    private ITeachingclassService teachingclassService;

    /**
     * 查询教学班信息列表
     */
    @PreAuthorize("@ss.hasPermi('teachingclass:teachingclass:list')")
    @GetMapping("/list")
    public TableDataInfo list(Teachingclass teachingclass)
    {

        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        boolean hasX = teacherId.contains("X");
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<Teachingclass> list = teachingclassService.selectTeachingclassList(teachingclass);
            return getDataTable(list);
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            startPage();
            List<Teachingclass> list = teachingclassService.selectTeachingclassList_handteacher(teacherId);
            return getDataTable(list);
        }
        else if(hasX)
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            startPage();
            List<Teachingclass> list = teachingclassService.selectTeachingclassList_Xiteacher(teacherId);
            return getDataTable(list);
        }
        else
        {
            //教师
            startPage();
            List<Teachingclass> list = teachingclassService.selectTeachingclassList_teacher(teacherId);
            return getDataTable(list);
        }

    }

    /**
     * 导出教学班信息列表
     */
    @PreAuthorize("@ss.hasPermi('teachingclass:teachingclass:export')")
    @Log(title = "教学班信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Teachingclass teachingclass)
    {
        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        boolean hasX = teacherId.contains("X");
        if("admin".equals(teacherId))
        {
            //管理员
            List<Teachingclass> list = teachingclassService.selectTeachingclassList(teachingclass);
            ExcelUtil<Teachingclass> util = new ExcelUtil<Teachingclass>(Teachingclass.class);
            util.exportExcel(response, list, "教学班信息数据");
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            List<Teachingclass> list = teachingclassService.selectTeachingclassList_handteacher(teacherId);
            ExcelUtil<Teachingclass> util = new ExcelUtil<Teachingclass>(Teachingclass.class);
            util.exportExcel(response, list, "教学班信息数据");
        }
        else if(hasX)
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            List<Teachingclass> list = teachingclassService.selectTeachingclassList_Xiteacher(teacherId);
            ExcelUtil<Teachingclass> util = new ExcelUtil<Teachingclass>(Teachingclass.class);
            util.exportExcel(response, list, "教学班信息数据");
        }
        else
        {
            //教师
            List<Teachingclass> list = teachingclassService.selectTeachingclassList_teacher(teacherId);
            ExcelUtil<Teachingclass> util = new ExcelUtil<Teachingclass>(Teachingclass.class);
            util.exportExcel(response, list, "教学班信息数据");
        }
    }

    /**
     * 获取教学班信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('teachingclass:teachingclass:query')")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId)
    {
        return success(teachingclassService.selectTeachingclassByClassId(classId));
    }

    /**
     * 新增教学班信息
     */
    @PreAuthorize("@ss.hasPermi('teachingclass:teachingclass:add')")
    @Log(title = "教学班信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Teachingclass teachingclass)
    {
        return toAjax(teachingclassService.insertTeachingclass(teachingclass));
    }

    /**
     * 修改教学班信息
     */
    @PreAuthorize("@ss.hasPermi('teachingclass:teachingclass:edit')")
    @Log(title = "教学班信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Teachingclass teachingclass)
    {
        return toAjax(teachingclassService.updateTeachingclass(teachingclass));
    }

    /**
     * 删除教学班信息
     */
    @PreAuthorize("@ss.hasPermi('teachingclass:teachingclass:remove')")
    @Log(title = "教学班信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds)
    {
        return toAjax(teachingclassService.deleteTeachingclassByClassIds(classIds));
    }


    @GetMapping("/teacher")
    public ResponseEntity<List<TeachingClassVO>> getClassesByTeacher(Authentication authentication) {
        String teacherId = authentication.getName();
        List<TeachingClassVO> classes = teachingclassService.getClassesByTeacher(teacherId);
        return ResponseEntity.ok(classes);
    }


    @GetMapping("/{classId}/students")
    public ResponseEntity<List<StudentVO>> getClassStudents(
            @PathVariable Integer classId) {
        List<StudentVO> students = teachingclassService.getClassStudents(classId);
        return ResponseEntity.ok(students);
    }
}
